#include "stream.h"
#include "app_params.h"
#include "wnds_mgmt.h"
#include "process_mgmt.h"
#include "lowlevel_hooks.h"
#include "time_mgmt.h"

#define WIN_OS               1
//test1
//test5
//testORG

using namespace std;


DWORD WINAPI watingInterr(LPVOID lpParam) 
{
    processes::thread_ready();

    window_info wndParams("MaskLayerClass1", "MaskLayer1", WndProc1, 0, 0, 110, 110);
    window_create new_window(wndParams, window_create::WND_DEP_NOTDEPLOY, window_create::WND_VIS_OPAQUE, window_create::WND_STY_NORMAL);
    new_window.create();

    wndParams = window_info("MaskLayerClass1", "MaskLayer2", WndProc1, 200, 200, 110, 110);
    new_window = window_create(wndParams, window_create::WND_DEP_NOTDEPLOY, window_create::WND_VIS_OPAQUE, window_create::WND_STY_NORMAL);
    new_window.create();

    wndParams = window_info("MaskLayerClass1", "MaskLayer3", WndProc2, 500, 500, 200, 200);
    new_window = window_create(wndParams, window_create::WND_DEP_DEPLOY, window_create::WND_VIS_OPAQUE, window_create::WND_STY_NORMAL);
    new_window.create();

    while(1);

    test();

    while(1);
    //Sleep(2000);

    return EXIT_SUCCESS;
}


DWORD WINAPI disableFullscreen(LPVOID lpParam) 
{
    processes::thread_ready();

    //stream::system_message("main", "Curr thread2:: "+decToHexString(GetCurrentThreadId())+" | "+decToHexString(processes::thread_get_current()) );

    UINT_PTR hhkKybd  = hooks::hook_create(hooks::HOOK_KEYBOARD, LowLevelKeyboardProc);

    window_info wndParams("MaskLayerClass", "MaskLayer", WndProc, 0, 0, 0, 0);
    window_create new_window(wndParams, window_create::WND_DEP_DEPLOY, window_create::WND_VIS_TRANSPARENT, window_create::WND_STY_MAXIMIZED_POPUP);

    OUTSTREAM<<endl<<"a1. "<<" wClassName: "<<wcToString(new_window.new_window.wClassName)<<" wcNp: "<<new_window.new_window.wClassName
             <<" wTitleName: "<<wcToString(new_window.new_window.wTitleName)<<" wtNp: "<<new_window.new_window.wTitleName<<endl; PRINTOUT;

    new_window.create();

    //HWND hConsole = GetConsoleWindow();
    //if( hConsole != GetForegroundWindow() )
    //    SetForegroundWindow(hConsole);

    hooks::hook_destroy(hhkKybd);
    while(1);

    return EXIT_SUCCESS;
}

INT main(INT argc, CHAR *argv[])
{
    UINT_PTR hThread1   = NULL;
    UINT_PTR hThread2   = NULL;
    string displayTextBuffer = "TeSt...";
    UINT_PTR hPipeNamedRaw = NULL;
    app_parameters main_parameters = get_main_parameters(argc, argv);

    if(main_parameters.getPipeName() != UNNAMED) {
       displayTextBuffer = "";
       hPipeNamedRaw = processes::pipe_named_open(FALSE, main_parameters.getPipeName());
        while(1) {
            processes::pipe_named_read(hPipeNamedRaw, displayTextBuffer);
            OUTSTREAM<<endl<<"text: "<<displayTextBuffer<<endl; PRINTOUT;
            if(displayTextBuffer != "")
                break;
        }
    }
    //else
        //processes::pipe_unnamed_write(pipeOutRaw, faulure_msg);

    //stream::system_message("main", "Curr thread1:: "+decToHexString(GetCurrentThreadId())+" | "+decToHexString(processes::thread_get_current()) );
    //hThread1 = processes::thread_create(watingInterr, NULL, 0);
    hThread2 = processes::thread_create(disableFullscreen, NULL, 0);

    //Sleep(3000);
    window_search search = window_search();
    HWND maskWnd = 0;
    while(maskWnd == 0)
        maskWnd = search.getWndHandle(window_info("MaskLayerClass", "MaskLayer"));

    stream::system_message("main", "Curr hwnd:: "+decToHexString((UINT)maskWnd) );

    UINT i = 0;
    while(1){
        INT cycle_timer = timers::timeout_peek_create(1000);

        window_text new_text((UINT_PTR)maskWnd);

        if(processes::thread_wait(hThread2, 0) == FUNCTION_SUCCESS)
            break;
        else {
            setWndProcUpdateBlockade(FALSE);
            new_text.printTextRandomPosition(20, TRUE, FALSE, FALSE,  displayTextBuffer);
            setWndProcUpdateBlockade(TRUE);

            if(hPipeNamedRaw != NULL) {
                do {
                    processes::pipe_named_read(hPipeNamedRaw, displayTextBuffer);
                }while(processes::pipe_named_write(hPipeNamedRaw, "Iter: "+decToString(++i)) == FUNCTION_FAILURE);
            }
        }

        //timers::timeout_create(1000);

        while(1) {
            INT timer = timers::timeout_peek_left_ms(cycle_timer);
            if( timer <= 0 )
                break;
            Sleep(10);
        }
    }

#if WIN_OS
        system("cls");
#elif UNIX
        system("clear");
#endif
    //ShowWindow(tray, SW_SHOW);
    OUTSTREAM<<"Logging in..."; PRINTOUT;
    //Sleep(2000);

    return EXIT_SUCCESS;
}

